package secret.santa.client;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Secret_Santa implements EntryPoint 
{
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final SantaServiceAsync santaService = GWT
			.create(SantaService.class);
	
	private SecretBox secret;
	private LoginPanel login;
	private AdminPanel admin;
	private LoaderPopupPanel loader;
	//private UserWindow user;
	
	private Long sid;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() 
	{
		login = new LoginPanel(this);

		RootPanel.get().add(login);
		
		loader = new LoaderPopupPanel();
		

		// Create the popup dialog box
		secret = new SecretBox(this);
	}
	public void addUser(String username, String password, String fullName, String email)
	{
		santaService.addUser(sid, username, password, fullName, email, new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) 
			{
				
			}

			@Override
			public void onSuccess(Boolean result) 
			{
				if(result)
					getUsers();
			}
		});
		
		
	}
	
	/**
	 * Send a login message to the server.
	 * @param username A String containing the user name.
	 * @param password A String containing the password.
	 */

	public void login(String username, String password) 
	{
		loader.show();
		if(username.equals("admin"))
		{
			santaService.login(username, password,
			new AsyncCallback<Long>() 
			{
				public void onFailure(Throwable caught) 
				{
					login.notLoggedIn();
					loader.hide();
					
				}

				public void onSuccess(Long sid) 
				{
					if(sid != 0L)
					{
						
						setSID(sid);
						
						createAdminWindow();
						login.loggedIn();
					}
					else
					{
						login.loggedIn();
						loader.hide();
					}
				}

				

				
			});
		}
		else
		{
			santaService.login(username, password,
				new AsyncCallback<Long>() 
				{
					public void onFailure(Throwable caught) 
					{
						login.loggedIn();
						loader.hide();
						
					}

					public void onSuccess(Long sid) 
					{
						if(sid != 0L)
						{
							
							setSID(sid);
							getSantaSecret();
							login.loggedIn();
						}
						else
						{
							login.notLoggedIn();
							login.setStatus("Login of paswoord verkeerd, probeer opnieuw.");
							loader.hide();
						}
					}

					
				});
		}
			
		
	}
	protected void createAdminWindow() 
	{
		admin = new AdminPanel(this);
		RootPanel.get().add(admin);
		
		getEvents();
		getUsers();
		
	}
	private void setSID(Long s) {
		sid = s;
	}
	private void relog()
	{
		login = new LoginPanel(this);

		RootPanel.get().add(login);	
	}
	private void getSantaSecret() 
	{
		santaService.getSecretData(sid, new AsyncCallback<SecretData>() 
		{

			@Override
			public void onFailure(Throwable caught) 
			{
				// TODO Auto-generated method stub
			}

			@Override
			public void onSuccess(SecretData result) 
			{
				loader.hide();
				secret.displayData(result);
			}
		});
		
	}
	

	public void getEvents() 
	{
		santaService.getEvents(sid, new AsyncCallback<CS_Event[]>() 
		{
			public void onFailure(Throwable caught) 
			{
				
			}

			public void onSuccess(CS_Event[] e) 
			{
				if(e == null)
					relog();

				else
					displayEvents(e);
			}
			
		});
		
	}
	public void getUsers() 
	{
		santaService.getUsers(sid, new AsyncCallback<CS_User[]>() 
		{
			public void onFailure(Throwable caught) 
			{
				
			}

			public void onSuccess(CS_User[] u) 
			{
				if(u == null)
					relog();

				else
					displayUsers(u);
			}
			
		});
		
	}
	private void displayUsers(CS_User[] u) 
	{
		if(admin == null)
		{
			admin = new AdminPanel(this);
			RootPanel.get().add(admin);
		}
		admin.displayUsers(u);
		
	}
	private void displayEvents(CS_Event[] e) 
	{
		loader.hide();
		if(admin == null)
		{
			admin = new AdminPanel(this);
			RootPanel.get().add(admin);
		}
		admin.displayEvents(e);
		
	}

	public void signOut() 
	{
		//TODO: implement signout
		
	}

	public void editUser(String key, String username, String password, String fullName, String email)
	{
		santaService.editUser(sid, key, username, password, fullName, email, new AsyncCallback<Boolean>() 
				{

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Boolean result) 
			{
				admin.userEdited();
				getUsers();
				
			}
		});
	}
	public void editEvent(String key, String name, String desc)
	{
		santaService.editEvent(sid, key, name, desc, new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Boolean result) 
			{
				admin.eventEdited();
				getEvents();
				
			}
		});
	}
	public void addEvent(String name, String desc) 
	{
		santaService.addEvent(sid, name, desc, new AsyncCallback<Boolean>()
		{
			public void onFailure(Throwable caught) 
			{
				
			}

			public void onSuccess(Boolean b) 
			{
				if(b)
					getEvents();
			}
			
		});
				
	}
	public void deleteUser(String key) 
	{
		santaService.removeUser(sid, key, new AsyncCallback<Boolean>() 
		{

		@Override
		public void onFailure(Throwable caught) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onSuccess(Boolean result) 
		{
			admin.userDeleted();
			getUsers();
			
		}
		
		});
	}
	public void deleteEvent(String key) 
	{
		santaService.removeEvent(sid, key, new AsyncCallback<Boolean>() 
		{

			@Override
			public void onFailure(Throwable caught)
			{
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Boolean result) 
			{
				admin.eventDeleted();
				getEvents();
				
			}
		});
	}
	public void queryForEvent(String key) 
	{
		santaService.queryEvent(sid, key, new AsyncCallback<EventQuery>() 
		{

			@Override
			public void onFailure(Throwable caught) 
			{
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(EventQuery result) 
			{
				admin.displayEventQuery(result);
				
			}
			
		});
		
		
	}
	public void addUsersToEvent(String[] selectedItems, String eventKey) 
	{
		santaService.addUsersToEvent(sid, selectedItems, eventKey, new AsyncCallback<Boolean>() 
		{

			@Override
			public void onFailure(Throwable caught) 
			{
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Boolean result)
			{
				admin.queryForEvent();
				
			}
		});
		
	}
	public void makeCouple(String encodedKey, String encodedKey2) 
	{
		santaService.makeCouple(sid, encodedKey, encodedKey2, new AsyncCallback<Boolean>() 
		{

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Boolean result) 
			{
				getUsers();
				
			}
		});
		
	}
	public void generateList(String eventKey) 
	{
		santaService.generateList(sid, eventKey, new AsyncCallback<Boolean>()
		{

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Boolean result) 
			{
				admin.queryForEvent();
				
			}
		});
		
	}
	public void editWishList(String entryKey, String wishList) 
	{
		santaService.editWishList(sid, entryKey, wishList, new AsyncCallback<Boolean>()
		{

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Boolean result) 
			{
				if(result)
					secret.setStatus("Lijstje verstuurd.");
				
			}
		});
		
		
	}
	public void logout()
	{
		sid = 0L;
		RootPanel.get().add(login);
		
	}
	public CS_User[] getUserData() 
	{
		return admin.getUserData();
	}
	public void deleteUsers(String[] keys) 
	{
		santaService.removeUsers(sid, keys, new AsyncCallback<Boolean>()
		{

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Boolean result) 
			{
				admin.userDeleted();
				getUsers();
			}
		});
		
		
	}
}
